﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

namespace Player
{

    [System.Serializable]
    public struct STHexagonPosData
    {
        /// <summary>
        /// 3的平方根
        /// </summary>
        private const float SQUARE_ROOT_OF_3 = 1.7320508f;

        public int nRowCount, nColumnCount;//行，列
        public float fNodeSize;//六边形边长

        /// <summary>
        /// 
        /// </summary>
        /// <param name="row">行数</param>
        /// <param name="column">列数</param>
        /// <param name="nodeSize">边长</param>
        public STHexagonPosData(int row, int column, float nodeSize = 1)
        {
            nRowCount = row;
            nColumnCount = column;
            fNodeSize = nodeSize;
        }

        public Vector2Int[] GetRoundPos(Vector2Int center)
        {
            List<Vector2Int> tmp = new List<Vector2Int>();
            int offset = center.x % 2 == 0 ? -1 : 1;//偶数行上下的 x 是i-1,i。奇数行上下的 x 是 i，i+1
                                                    //左
            tmp.Add(new Vector2Int() { x = center.x - 1, y = center.y });
            tmp.Add(new Vector2Int() { x = center.x - 1, y = center.y + offset });
            //上下
            tmp.Add(new Vector2Int() { x = center.x, y = center.y + 1 });
            tmp.Add(new Vector2Int() { x = center.x, y = center.y - 1 });
            //右
            tmp.Add(new Vector2Int() { x = center.x + 1, y = center.y });
            tmp.Add(new Vector2Int() { x = center.x + 1, y = center.y + offset });

            //去除越界的坐标
            for (var i = 0; i < tmp.Count; i++)
            {
                if (tmp[i].x < 0 || tmp[i].x >= nRowCount ||
                    tmp[i].y < 0 || tmp[i].y >= nColumnCount)
                {
                    tmp.RemoveAt(i);
                    i--;
                }
            }
            return tmp.ToArray();
        }

        public Vector2Int[] GetRoundPos(Vector2Int center,int range)
        {
            //TODO 暴力处理
            if (range < 1) return null;
            //中心1个，第一圈6个 1*6，第二圈12个 2*6，第三圈18个 3*6
            List<Vector2Int> list = new List<Vector2Int>();
            list.Add(center);
            int start = 0,end=1;
            //添加当前最外圈的格子的周围格子
            for (int i = 0; i < range; i++)
            {
                //标记一下前面已经添加过的格子
                for (int j = start; j < end; j++)
                {
                    foreach (var pos in GetRoundPos(list[j]))
                    {
                        if (!list.Contains(pos)) list.Add(pos);
                    }
                }
                start = end;
                end = list.Count;
            }

            //去中心点
            list.Remove(center);
            return list.ToArray();
        }

        public Vector2 GetPos(Vector2Int posIndex)
        {
            //横着摆，奇数行会错位
            Vector2 offset = new Vector2();
            if (posIndex.x % 2 == 1)
                offset.y = SQUARE_ROOT_OF_3 / 2f;

            Vector2 pos = new Vector2()
            {
                x = posIndex.x * 1.5f,
                y = posIndex.y * SQUARE_ROOT_OF_3,
            };

            pos += offset;//加六边形组合偏移
            pos *= fNodeSize;//乘边长

            return pos;
        }
        public Vector2Int GetIndex(Vector2 pos)
        {
            Vector2Int index = Vector2Int.zero;
            index.x = Mathf.RoundToInt(pos.x / fNodeSize / 1.5f);

            //横着摆，奇数行会错位
            float offsetY = 0;
            if (index.x % 2 == 1)
                offsetY = SQUARE_ROOT_OF_3 / 2f;

            index.y = Mathf.RoundToInt((pos.y / fNodeSize - offsetY) / SQUARE_ROOT_OF_3);

            return index;
        }
    }

}